package pxene.phantomJs.util;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class DriverUtils {
	/**
	 * 整理元素
	 * @param driver
	 * @param selector
	 * @return
	 */
	public static  boolean doesWebElementExist(WebDriver driver, By selector) {
		try {
			driver.findElement(selector);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	public static String getAttrValue(WebDriver driver,By selector,String attr){
		boolean flag = doesWebElementExist(driver, selector);
		if(flag){
			return driver.findElement(selector).getAttribute(attr);
		}else{
			return null;
		}
	}
	
	public static String getText(WebDriver driver,By selector){
		boolean flag = doesWebElementExist(driver, selector);
		if(flag){
			return driver.findElement(selector).getText();
		}else{
			return null;
		}
	}
	/**
	 * 局部元素
	 * @param element
	 * @param selector
	 * @return
	 */
	
	
	public static  boolean doesElemExist(WebElement element, By selector) {
		try {
			element.findElement(selector);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	
	public static String getElemAttr(WebElement element,By selector,String attr){
		boolean flag = doesElemExist(element, selector);
		if(flag){
			return element.findElement(selector).getAttribute(attr);
		}else{
			return null;
		}
	}
	
	public static String getElemText(WebElement element,By selector){
		boolean flag = doesElemExist(element, selector);
		if(flag){
			return element.findElement(selector).getText();
		}else{
			return null;
		}
	}
}
